package com.kchouse.mine;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kchouse.MainActivity;
import com.kchouse.R;
import com.kchouse.bean.MerchantItem;
import com.kchouse.bean.OrderEvaItem;
import com.kchouse.common.API;
import com.kchouse.common.BaseActivity;
import com.kchouse.common.MyHttpCycleContext;
import com.kchouse.common.ToastUtils;
import com.kchouse.union.MerchantDetailsActivity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

public class CollectionBusinessesActivity extends BaseActivity implements View.OnClickListener {

    private CollectionBusinessesActivity mContent;
    private TextView tv_title;
    private ImageView iv_back;
    private ListView lv_item;
    private Intent intent;
    private ArrayList<MerchantItem> listfocus = new ArrayList<>();
    private CBAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection_businesses);

        mContent = this;
        intiData();
        intiView();
    }

    private void intiView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("关注商家");
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);

        lv_item = (ListView) findViewById(R.id.lv_item);
        adapter = new CBAdapter();
        lv_item.setAdapter(adapter);
        lv_item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MerchantItem item = listfocus.get(position);
//                        ToastUtils.getInstanc(getActivity()).showToast("商家id："+item.getId());
                intent.putExtra("Merchantid", item.getId());
                intent.setClass(mContent, MerchantDetailsActivity.class);
                startActivity(intent);
            }
        });
    }

    private void intiData() {
        intent = getIntent();

        GetFocusList();
    }


    /**
     * 消费与评价列表
     *
     * @param
     */
    private void GetFocusList() {
        String url = API.GetFocusList + MainActivity.userid;
        RequestParams params = new RequestParams(new MyHttpCycleContext());//请求参数
        params.addFormDataPart("userId", MainActivity.userid);
        params.addHeader("token", MainActivity.token);//添加header信息

        HttpRequest.get(url, params, new BaseHttpRequestCallback<String>() {

            //请求网络前
            @Override
            public void onStart() {
                showdialog();
            }

            @Override
            protected void onSuccess(String str) {
                Log.e("==", str);
                JSONObject json = JSONObject.parseObject(str);
                if (json.getBoolean("successful")) {
                    String datastr = json.getString("data");
                    listfocus.addAll(JSONObject.parseArray(datastr, MerchantItem.class));
                    adapter.notifyDataSetChanged();
                } else {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    ToastUtils.getInstanc(mContent).showToast(data.getString("msg"));
                    if (data.getString("code").equals("401")) {
                        intent.setClass(mContent, LoginPhoneActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
            }


            //请求失败（服务返回非法JSON、服务器异常、网络异常）
            @Override
            public void onFailure(int errorCode, String msg) {
//                toast("网络异常~，请检查你的网络是否连接后再试");
                Log.e("==", msg);
            }

            //请求网络结束
            @Override
            public void onFinish() {
                dismissdialog();
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    class CBAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return listfocus.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MerchantItem item = listfocus.get(position);
            View view = getLayoutInflater().inflate(R.layout.lv_cb_item, null);
            ImageView iv_item = (ImageView) view.findViewById(R.id.iv_item);
            Picasso.with(mContent).load(item.getUrl()).error(R.mipmap.ic_loadingerror).into(iv_item);
            TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_name.setText(item.getShortName());
            return view;
        }
    }
}
